//游戏规则
var GAMERULE_KWX = {};
// 局数
GAMERULE_KWX.GAME_TYPE_ROUND_0				= 0x00000001;						//	局数 8局
GAMERULE_KWX.GAME_TYPE_ROUND_1				= 0x00000002;						//	局数 16局
// 玩家人数
GAMERULE_KWX.GAME_TYPE_P_COUNT_0		    = 0x00000004;						//	玩家人数 2人
GAMERULE_KWX.GAME_TYPE_P_COUNT_1		    = 0x00000008;						//	玩家人数 3人

//定漂方式
GAMERULE_KWX.GAME_TYPE_PIAO_MODE_NO			= 0x00000020;						//	定漂方式 不漂
GAMERULE_KWX.GAME_TYPE_PIAO_MODE_SEL_FIRST	= 0x00000040;						//	定漂方式 首局选漂
GAMERULE_KWX.GAME_TYPE_PIAO_MODE_SEL_EVERY	= 0x00000080;						//	定漂方式 每局选漂
GAMERULE_KWX.GAME_TYPE_PIAO_MODE_FIX		= 0x00000100;						//	定漂方式 固定漂
//固定漂数量
GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_1		= 0x00000200;						//	固定漂数量 1
GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_2		= 0x00000400;						//	固定漂数量 2
GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_3		= 0x00000800;						//	固定漂数量 3
GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_4		= 0x00001000;						//	固定漂数量 4
//亮牌时间
GAMERULE_KWX.GAME_TYPE_LIGHT_TIME			= 0x00000010;						//	轮到出牌时亮牌 （不带此规则时随时亮）
// 亮牌方式
GAMERULE_KWX.GAME_TYPE_LIGNT_MODE_ALL		= 0x00002000;						//	亮牌方式 全部亮
GAMERULE_KWX.GAME_TYPE_LIGNT_MODE_PART		= 0x00004000;						//	亮牌方式 部分亮
// 买码数量
GAMERULE_KWX.GAME_TYPE_MM_MODE_NO			= 0x00010000;						//	买码方式 不买码
GAMERULE_KWX.GAME_TYPE_MM_MODE_ZIMO			= 0x00020000;						//	买码方式 自摸买码
GAMERULE_KWX.GAME_TYPE_MM_MODE_LIGHT_ZIMO	= 0x00040000;						//	买码方式 亮牌自摸买码
// 买码数量
GAMERULE_KWX.GAME_TYPE_MM_CNT_ALONE			= 0x00080000;						//	买码方式 买独码
GAMERULE_KWX.GAME_TYPE_MM_CNT_1_GIVE_1		= 0x00100000;						//	买码方式 买一赠一
GAMERULE_KWX.GAME_TYPE_MM_CNT_1_ALWAYS		= 0x00200000;						//	买码方式 买一持续码
GAMERULE_KWX.GAME_TYPE_MM_CNT_5_JIN_10		= 0x00400000;						//	买码方式 买5进10
// 自动托管
GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_0		= 0x00800000;						//	自动托管 不托管
GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_1		= 0x01000000;						//	自动托管 15S
GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_2		= 0x02000000;						//	自动托管 20S
GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_3		= 0x04000000;						//	自动托管 30S
// 自动准备
GAMERULE_KWX.GAME_TYPE_AUTO_READY_0			= 0x08000000;						//	自动准备 8S
GAMERULE_KWX.GAME_TYPE_AUTO_READY_1			= 0x10000000;						//	自动准备 15S
GAMERULE_KWX.GAME_TYPE_AUTO_READY_2			= 0x20000000;						//	自动准备 20S
// 防作弊
GAMERULE_KWX.GAME_TYPE_IP_CHECK				= 0x40000000;						//	IP防作弊
GAMERULE_KWX.GAME_TYPE_GPS_CHECK			= 0x80000000;						//	GPS防作弊

cc.Class({
    extends: cc.BaseClass,

    properties: {
        atlas: cc.SpriteAtlas,
        nodes:[cc.Label], //[底分,选飘]
        scorllViews:[cc.ScrollView],
    },

    onLoad:function(){
        this.initData();
    },

    start () {
        this.showView();
    },

    initData:function(){
        this.m_togArr = null;
        this.m_togArr = this.node.getComponentsInChildren(cc.Toggle);
        this.m_cbNeedUpdate = 0;
        this.m_TargetToggle = null;
        this.m_rule = 0;
        this.m_isClickBaseScore = false;
        this.m_isClickPiaoScore = false;
        this.baseScode = [0.5,1,2,3,5,10,20,30];
        this.piaoScore = [1,2,3,4];

        var item =  JSON.parse(cc.sys.localStorage.getItem(GAMERILE +62007));
        if(!item){
            //默认规则
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_ROUND_0;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_P_COUNT_0;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_PIAO_MODE_NO;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_LIGHT_TIME;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_LIGNT_MODE_ALL;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_MM_MODE_NO;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_0;
            this.m_rule |= GAMERULE_KWX.GAME_TYPE_AUTO_READY_0;

            var baseScore = this.baseScode[0];
            var piaoScore = this.piaoScore[0];
        }else{
            this.m_rule = item.dwRules;
            var baseScore = item.baseScore;
            var piaoScore = this.piaoScore[0];
            if(this.m_rule |= GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_1){
                piaoScore = this.piaoScore[0];
            }else if(this.m_rule |= GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_2){
                piaoScore = this.piaoScore[1];
            }else if(this.m_rule |= GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_3){
                piaoScore = this.piaoScore[2];
            }else if(this.m_rule |= GAMERULE_KWX.GAME_TYPE_PIAO_FIX_CNT_4){
                piaoScore = this.piaoScore[3];
            }
        }

        var MMCount = this.m_mmCount = this.node.getChildByName("MMCount");
        if(this.m_rule |= GAMERULE_KWX.GAME_TYPE_MM_MODE_NO){
            MMCount.getComponent(cc.ToggleContainer).enabled = false;
        }

        this.nodes[0].string = baseScore;
        this.nodes[1].string = piaoScore;

        for(var j = 0; j < this.scorllViews.length; j++){
            this.scorllViews[j].node.active = false;
            var score = j == 0 ? this.baseScode : this.piaoScore;
            var content =  this.scorllViews[j].content;
            var item = content.children[0];
            for(var i =0; i < score.length-1;i++){
                var temp = cc.instantiate(item);
                content.addChild(temp);
            }
            for(var i =0; i < score.length;i++){
                var temp = content.children[i];
                temp.name = score[i].toString();
                temp.getChildByName("lab").getComponent(cc.Label).string = score[i];
            }
        }
    },

    showView:function(){
        if(!this.m_rule) return; 
        for(var i =0; i < this.m_togArr.length;i++){
            this.m_togArr[i].isChecked = false;
        }

        for(i = 0; i < this.m_togArr.length;i++){
            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_ROUND_0){
                this.m_togArr[0].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_ROUND_1){
                this.m_togArr[1].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_P_COUNT_0){
                this.m_togArr[2].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_P_COUNT_1){
                this.m_togArr[3].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_PIAO_MODE_NO){
                this.m_togArr[4].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_PIAO_MODE_SEL_FIRST){
                this.m_togArr[5].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_PIAO_MODE_SEL_EVERY){
                this.m_togArr[6].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_PIAO_MODE_FIX){
                this.m_togArr[7].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_LIGHT_TIME){
                this.m_togArr[8].isChecked = true;
            }else{
                this.m_togArr[9].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_LIGNT_MODE_ALL){
                this.m_togArr[10].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_LIGNT_MODE_PART){
                this.m_togArr[11].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_MODE_NO){
                this.m_togArr[12].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_MODE_ZIMO){
                this.m_togArr[13].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_MODE_LIGHT_ZIMO){
                this.m_togArr[14].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_CNT_ALONE){
                this.m_togArr[15].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_CNT_1_GIVE_1){
                this.m_togArr[16].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_CNT_1_ALWAYS){
                this.m_togArr[17].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_MM_CNT_5_JIN_10){
                this.m_togArr[18].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_0){
                this.m_togArr[19].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_1){
                this.m_togArr[20].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_2){
                this.m_togArr[21].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_TRUSTEE_3){
                this.m_togArr[22].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_READY_0){
                this.m_togArr[23].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_READY_1){
                this.m_togArr[24].isChecked = true;
            }else if(this.m_rule & GAMERULE_KWX.GAME_TYPE_AUTO_READY_2){
                this.m_togArr[25].isChecked = true;
            }

            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_IP_CHECK){
                this.m_togArr[26].isChecked = true;
            }
            if(this.m_rule & GAMERULE_KWX.GAME_TYPE_GPS_CHECK){
                this.m_togArr[27].isChecked = true;
            }

        }

        //为生效？？
        for(i = 0; i < this.m_togArr.length;i++){
            var color = this.m_togArr[i].isChecked ? cc.color(255,255,255) : cc.color(153,146,195);
            console.log("=========color=========="+ "a = " +color.a + " b = "+color.b +" g = " +color.g + "r = " +color.r);
            this.m_togArr[i].node.getChildByName("Label").color = color;
        }
    },

    onTogJuShu:function(){

    },

    onBtnClickCallBack:function(event,customData){
        var type = customData;
        var target = event.target;
        if(type == "baseScore"){
            this.onBaseScoreClick(target);
        }else if(type == "baseScoreItem"){
            this.setBaseScoreItemClick(0,target.name);
        }else if(type == "piaoScore"){
            this.setPiaoScoreItemClick(target);
        }else if(type == "piaoScoreItem"){
            this.setBaseScoreItemClick(1,target.name);
        }
    },

    onBaseScoreClick:function(target){
        var frame = this.atlas.getSpriteFrame("common_tog_up");
        if(!this.m_isClickBaseScore){
            frame = this.atlas.getSpriteFrame("common_tog_down");
        }
        if(frame){
            target.getComponent(cc.Sprite).spriteFrame = frame;
        }
        this.scorllViews[0].node.active = !this.m_isClickBaseScore;
        this.m_isClickBaseScore = !this.m_isClickBaseScore;
    },

    setPiaoScoreItemClick:function(target){
        var frame = this.atlas.getSpriteFrame("common_tog_up");
        if(!this.m_isClickPiaoScore){
            frame = this.atlas.getSpriteFrame("common_tog_down");
        }
        if(frame){
            target.getComponent(cc.Sprite).spriteFrame = frame;
        }
        this.scorllViews[1].node.active = !this.m_isClickPiaoScore;
        this.m_isClickPiaoScore = !this.m_isClickPiaoScore;
    },

    setBaseScoreItemClick:function(index,name){
        this.nodes[index].string = name;
    },

    onTogClickCallBack:function(event,customData){
        this.m_TargetToggle = null;
        this.m_cbNeedUpdate = 2;
    },

    update:function(){
        if(this.m_cbNeedUpdate > 0){
            this.m_cbNeedUpdate --;
        }else{
            this.m_TargetToggle = null;
            return;
        }
        for(let i in this.m_togArr){
            // if(!this.m_togArr[i].node.active) continue;
            var color = this.m_togArr[i].isChecked ? cc.color(255,255,255) : cc.color(153,146,195);
            this.m_togArr[i].node.getChildByName("Label").color = color;
        }
        // this.CheckGameRule();
    },

    //获取低分和规则
    getRuleAndBaseScore:function(){
        var obj = {
            dwRules     : this.m_rule,
            baseScore   : 2,
            wKindID     : KindID_KWX,
            szRoomName  : "快来玩吧",
        }
        // var dwRules = 0;
        // for (var i in this.m_togArr) {
        //     if (this.m_togArr[i].node.parent.active && this.m_togArr[i].node.active && this.m_togArr[i].isChecked) {
        //         var rulesIndex = parseInt(this.m_togArr[i].node.name);
        //         if (rulesIndex <= 31) obj += (1 << rulesIndex);
        //     }
        // }
        // //下拉框规则
        // for(var i in this.m_BoxArr){
        //     if(this.m_BoxArr[i]){
        //         if (this.m_BoxArr[i] <= 31) obj += (1 << this.m_BoxArr[i]);
        //     }
        // }

        return obj;
    },

    CheckGameRule:function(){
 
    },

    CheckRoomCard: function (objJuShu, objPlayer) {
        for (var i in this.m_togArr) {
            // 局数选择
            for (var j = 0; j < objJuShu.szNodeName.length; ++j) {
                if (this.m_togArr[i].node.name == objJuShu.szNodeName[j]) {
                    objJuShu.cbNodeIndex[j] = i;
                    if (this.m_togArr[i].isChecked) objJuShu.cbSelID = j;
                }
            }
            // 人数选择
            for (var j = 0; j < objPlayer.szNodeName.length; ++j) {
                if (this.m_togArr[i].node.name == objPlayer.szNodeName[j]) {
                    objPlayer.cbNodeIndex[j] = i;
                    if (this.m_togArr[i].isChecked) objPlayer.cbSelID = j;
                }
            }
        }

        var wRoomCard = objJuShu.wRoomCard[objJuShu.cbSelID];
        var wPCount = objPlayer.wPCount[objPlayer.cbSelID];

        for (var i = 0; i < objJuShu.cbNodeIndex.length; ++i) {
            var cbIndex = objJuShu.cbNodeIndex[i];
            var pLabel = this.$('Label@Label', this.m_togArr[cbIndex].node);
            if (pLabel) pLabel.string = '' + objJuShu.wJuCount[i] + '局（' + (objJuShu.wRoomCard[i] * wPCount) + '张）';
        }
    },

    CheckKWX: function () {
        if (this.m_KindID != KindID_KWX) return;

        if(!this.m_CustomBoxKWX) {
            this.m_CustomBoxKWX = this.$('Other/CustomBox@CustomBox');
            this.m_CustomBoxKWX.SetHook(this,function(Data) { this.setBoxRule(Data); }.bind(this));

            var DataArray = new Array();
            var temp = [1,2,3,4];
            var tempRule = [9,10,11,12];
            for(var i in temp) {
                DataArray.push({
                    KindID: i,
                    Content: temp[i],
                    RuleID:tempRule[i],
                    Group:0,
                });
            }
            this.m_CustomBoxKWX.SetData(DataArray);
        }

    },

});
